package org.huangrui.spark.java.core.acc;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.broadcast.Broadcast;

import java.util.Arrays;
import java.util.List;

/**
 * @Author hr
 * @Create 2024-10-20 1:40
 */
public class Spark05_Bc {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);

        final JavaRDD<String> rdd = jsc.parallelize(
                Arrays.asList("Hello", "Spark", "Hadoop", "Flink", "Spark", "Hadoop")
        );

        List<String> okList = Arrays.asList("Spark", "Hadoop");
        // TODO 默认数据传输以Task为单位进行传输，如果想要以Executor为单位传输，那么需要进行包装（封装）
        Broadcast<List<String>> broadcast = jsc.broadcast(okList);

        // JavaRDD<String> filter = rdd.filter(s -> !okList.contains(s));
        JavaRDD<String> filter = rdd.filter(s -> !broadcast.getValue().contains(s));

        filter.collect().forEach(System.out::println);

        jsc.close();
    }
}
